package com.example.suze.libraryinformationmanagementsystem.Fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.suze.libraryinformationmanagementsystem.Activity.LoginActivity;
import com.example.suze.libraryinformationmanagementsystem.Activity.MainActivity;
import com.example.suze.libraryinformationmanagementsystem.Activity.Search;
import com.example.suze.libraryinformationmanagementsystem.Adapter.BookListAdapter;
import com.example.suze.libraryinformationmanagementsystem.R;
import com.example.suze.libraryinformationmanagementsystem.net.API;
import com.example.suze.libraryinformationmanagementsystem.net.JSONCallBack;
import com.example.suze.libraryinformationmanagementsystem.net.entiry.BookDatum;
import com.example.suze.libraryinformationmanagementsystem.net.entiry.BookRoot;
import com.example.suze.libraryinformationmanagementsystem.net.entiry.ReturnStateEntity;
import com.person.dao.BookDao;
import com.person.dao.DBManager;
import com.person.dao.PersonDao;
import com.person.entity.Book;

import java.util.ArrayList;
import java.util.List;

import static com.example.suze.libraryinformationmanagementsystem.R.id.list_book;

/**
 * Created by suze on 2017/6/10.
 */

public class ClassificationFragment extends Fragment implements View.OnClickListener{
    private TextView computer,english,chinese,art,science,children,life,philosophy,tourism,architecture;//图书分类
    private List<BookDatum> books = new ArrayList<>();
    private ListView book;
    private JSONCallBack callBack;
    private MainActivity mainActivity;
    private API api;
    private String tag = "小说";
    private final int UPDATE_ACCOUNT = 2;


    //适配;线程
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == UPDATE_ACCOUNT) {
                int arg = msg.arg1;
                if (arg == 1) {
                    BookListAdapter booklistadapter = new BookListAdapter(mainActivity,books);
                    book.setAdapter(booklistadapter);
                }
            }
        }
    };


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        super.onCreateView(inflater, container, savedInstanceState);
        View chatView = inflater.inflate(R.layout.fragment_classification, container,false);
        return chatView;
    }
    @Override
    public void onActivityCreated(Bundle savedInstanceState){
        mainActivity = (MainActivity) getActivity();
        api = API.getAPI(mainActivity);
        net();
        computer = (TextView) getView().findViewById(R.id.computer);
        english = (TextView) getView().findViewById(R.id.english);
        chinese = (TextView) getView().findViewById(R.id.chinese);
        art = (TextView) getView().findViewById(R.id.art);
        science = (TextView) getView().findViewById(R.id.science);
        children = (TextView) getView().findViewById(R.id.children);
        life = (TextView) getView().findViewById(R.id.life);
        philosophy = (TextView) getView().findViewById(R.id.philosophy);
        tourism = (TextView) getView().findViewById(R.id.tourism);
        architecture = (TextView) getView().findViewById(R.id.architecture);
        book = (ListView) getView().findViewById(list_book);
        computer.setOnClickListener(this);
        english.setOnClickListener(this);
        chinese.setOnClickListener(this);
        art.setOnClickListener(this);
        science.setOnClickListener(this);
        children.setOnClickListener(this);
        life.setOnClickListener(this);
        philosophy.setOnClickListener(this);
        tourism.setOnClickListener(this);
        architecture.setOnClickListener(this);
        initColor();
        computer.setBackgroundColor(0xffff0000);
//        DBManager dbManager = new DBManager(mainactivity);
//        BookDao bookdao = dbManager.getDaoSession().getBookDao();
//        books = bookdao.queryBuilder().where(BookDao.Properties.Tag.eq(1)).build().list();
//        if(books!=null&&books.size()>0){
//            BookListAdapter booklistadapter = new BookListAdapter(mainactivity,books);
//            book.setAdapter(booklistadapter);}
        book.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(mainActivity, Search.class);
                Bundle bundle = new Bundle();
                bundle.putString("name",books.get(position).getbookname());
                bundle.putString("author",books.get(position).getauthor());
                bundle.putString("abstract1",books.get(position).getabstract1());
                bundle.putString("publishing",books.get(position).getpublishing());
                if(books.get(position).getis_lend()){
                    bundle.putInt("status",1);
                }else {
                    bundle.putInt("status", 0);

                }
                bundle.putLong("borrowerId",5120145303l);
                bundle.putString("id",books.get(position).getNo());
                bundle.putString("img",books.get(position).getimg());
                //bundle.putString("img",books.get(position).getImg());
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onResume() {

        super.onResume();
    }

    @Override
    public void onClick(View v) {
//        MainActivity mainactivity = (MainActivity) getActivity();
//        DBManager dbManager = new DBManager(mainactivity);
//        BookDao bookdao = dbManager.getDaoSession().getBookDao();
        switch (v.getId()) {
            case R.id.computer:
                initColor();
                computer.setBackgroundColor(0xffff0000);
                tag = "小说";
                net();
                break;
            case R.id.english:
                initColor();
                english.setBackgroundColor(0xffff0000);
                tag = "外语";
                net();
                break;
            case R.id.chinese:
                initColor();
                chinese.setBackgroundColor(0xffff0000);
                tag = "文学";
                net();
                break;
            case R.id.art:
                initColor();
                art.setBackgroundColor(0xffff0000);
                tag = "艺术";
                net();
                break;
            case R.id.science:
                initColor();
                science.setBackgroundColor(0xffff0000);
                tag = "科学";
                net();
                break;
            case R.id.children:
                initColor();
                children.setBackgroundColor(0xffff0000);
                tag = "少儿";
                net();
                break;
            case R.id.life:
                initColor();
                life.setBackgroundColor(0xffff0000);
                tag = "生活";
                net();
                break;
            case R.id.philosophy:
                initColor();
                philosophy.setBackgroundColor(0xffff0000);
                tag = "哲学";
                net();
                break;
            case R.id.tourism:
                initColor();
                tourism.setBackgroundColor(0xffff0000);
                tag = "旅游";
                net();
                break;
            case R.id.architecture:
                initColor();
                architecture.setBackgroundColor(0xffff0000);
                tag = "建筑";
                net();
                break;
        }
    }
    private void initColor(){
        computer.setBackgroundColor(0xff999999);
        english.setBackgroundColor(0xff999999);
        chinese.setBackgroundColor(0xff999999);
        art.setBackgroundColor(0xff999999);
        science.setBackgroundColor(0xff999999);
        children.setBackgroundColor(0xff999999);
        life.setBackgroundColor(0xff999999);
        philosophy.setBackgroundColor(0xff999999);
        tourism.setBackgroundColor(0xff999999);
        architecture.setBackgroundColor(0xff999999);
    }
    private void net(){
        callBack = new JSONCallBack<BookRoot>(new BookRoot()) {
            @Override
            public void onSuccess(BookRoot entity) {
                if(entity.getstate().equals("success") && entity.getcode() == 0){
                    if(entity.getdata().size()>0){
                        books.clear();
                        for(int i =0;i<entity.getdata().size();i++)
                        {
                            books.add(entity.getdata().get(i));
                        }
                        Thread thread = new Thread(new Runnable() {
                            @Override
                            public void run() {
                                Message message = new Message();
                                message.arg1 = 1;
                                message.what = UPDATE_ACCOUNT;
                                handler.sendMessage(message);
                            }
                        });
                        thread.start();
                    }
                }else{
                    Looper.prepare();
                    Toast.makeText(mainActivity, "请求数据失败", Toast.LENGTH_LONG).show();
                    Looper.loop();
                }
            }

        };
        api.sqlOporate("select", "SELECT * FROM book WHERE tag = '"+tag+"'", callBack);
    }

}
